package org.argus.job.server.system.workflow.validator;

import java.util.Objects;
import java.util.Optional;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.argus.common.core.enums.StatusEnum;
import org.argus.common.core.exception.ServiceException;
import org.argus.common.core.utils.JsonUtils;
import org.argus.job.common.enums.WorkflowNodeType;
import org.argus.job.common.model.PEWorkflowDAG;
import org.argus.job.server.system.domain.WorkflowInfoEntity;
import org.argus.job.server.system.domain.WorkflowNodeInfoEntity;
import org.argus.job.server.system.workflow.algorithm.WorkflowDAG;
import org.argus.job.server.system.workflow.mapper.WorkflowInfoMapper;
import org.argus.job.server.system.workflow.mapper.WorkflowNodeInfoMapper;
import org.springframework.stereotype.Component;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Echo009
 * @since 2021/12/14
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class NestedWorkflowNodeValidator implements NodeValidator {

    private final WorkflowInfoMapper workflowInfoMapper;

    private final WorkflowNodeInfoMapper workflowNodeInfoMapper;

    @Override
    public void complexValidate(WorkflowNodeInfoEntity node, WorkflowDAG dag) {
        // 这里检查是否循环嵌套(自身引用自身)
        if (Objects.equals(node.getJobId(), node.getWorkflowId())) {
            throw new ServiceException("Illegal nested workflow node,Prohibit circular references!" + node.getNodeName());
        }
    }


    @Override
    public void simpleValidate(WorkflowNodeInfoEntity node) {
        // 判断对应工作流是否存在
        WorkflowInfoEntity workflowInfo = workflowInfoMapper.selectById(node.getJobId());
        if (workflowInfo == null) {
            throw new ServiceException("Illegal nested workflow node,specified workflow is not exist,node name : " + node.getNodeName());
        }

        if (workflowInfo.getStatus() == StatusEnum.DELETED) {
            throw new ServiceException("Illegal nested workflow node,specified workflow has been deleted,node name : " + node.getNodeName());
        }
        // 不允许多层嵌套，即 嵌套工作流节点引用的工作流中不能包含嵌套节点
        PEWorkflowDAG peDag = null;
        try {
            peDag = JsonUtils.parseObject(workflowInfo.getPeDAG(), PEWorkflowDAG.class);
        } catch (JsonProcessingException e) {
            log.error("parse peDAG error", e);
            throw new ServiceException("Illegal nested workflow node,specified workflow is invalidate,node name : " + node.getNodeName());
        }
        for (PEWorkflowDAG.Node peDagNode : peDag.getNodes()) {
            //
            WorkflowNodeInfoEntity nestWfNode = workflowNodeInfoMapper.selectById(peDagNode.getNodeId());
            if (nestWfNode == null) {
                // 嵌套的工作流无效，缺失节点元数据
                throw new ServiceException("Illegal nested workflow node,specified workflow is invalidate,node name : " + node.getNodeName());
            }
            if (Objects.equals(nestWfNode.getType(), WorkflowNodeType.NESTED_WORKFLOW.getCode())) {
                throw new ServiceException("Illegal nested workflow node,specified workflow must be a simple workflow,node name : " + node.getNodeName());
            }
        }
    }

    @Override
    public WorkflowNodeType matchingType() {
        return WorkflowNodeType.NESTED_WORKFLOW;
    }
}
